<template>
  <el-dialog
    :title="!dataForm.shopId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="展示图" prop="imgUrl">
      <el-input v-model="dataForm.imgUrl" placeholder="展示图"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="shopName">
      <el-input v-model="dataForm.shopName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品售价" prop="price">
      <el-input v-model="dataForm.price" placeholder="商品售价"></el-input>
    </el-form-item>
    <el-form-item label="数量" prop="count">
      <el-input v-model="dataForm.count" placeholder="数量"></el-input>
    </el-form-item>
    <el-form-item label="库存" prop="total">
      <el-input v-model="dataForm.total" placeholder="库存"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="商品状态 0 出售中 1 仓库中 2 已售出 3 回收站" prop="state">
      <el-input v-model="dataForm.state" placeholder="商品状态 0 出售中 1 仓库中 2 已售出 3 回收站"></el-input>
    </el-form-item>
    <el-form-item label="是否上架 0 未上架 1 上架" prop="status">
      <el-input v-model="dataForm.status" placeholder="是否上架 0 未上架 1 上架"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="是否删除" prop="delStatus">
      <el-input v-model="dataForm.delStatus" placeholder="是否删除"></el-input>
    </el-form-item>
    <el-form-item label="分类id" prop="categoryId">
      <el-input v-model="dataForm.categoryId" placeholder="分类id"></el-input>
    </el-form-item>
    <el-form-item label="关键字" prop="keyword">
      <el-input v-model="dataForm.keyword" placeholder="关键字"></el-input>
    </el-form-item>
    <el-form-item label="商品简介" prop="introduce">
      <el-input v-model="dataForm.introduce" placeholder="商品简介"></el-input>
    </el-form-item>
    <el-form-item label="单位" prop="unit">
      <el-input v-model="dataForm.unit" placeholder="单位"></el-input>
    </el-form-item>
    <el-form-item label="是否包邮" prop="isPackage">
      <el-input v-model="dataForm.isPackage" placeholder="是否包邮"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          shopId: 0,
          imgUrl: '',
          shopName: '',
          price: '',
          count: '',
          total: '',
          sort: '',
          state: '',
          status: '',
          createTime: '',
          updateTime: '',
          delStatus: '',
          categoryId: '',
          keyword: '',
          introduce: '',
          unit: '',
          isPackage: ''
        },
        dataRule: {
          imgUrl: [
            { required: true, message: '展示图不能为空', trigger: 'blur' }
          ],
          shopName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '商品售价不能为空', trigger: 'blur' }
          ],
          count: [
            { required: true, message: '数量不能为空', trigger: 'blur' }
          ],
          total: [
            { required: true, message: '库存不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '商品状态 0 出售中 1 仓库中 2 已售出 3 回收站不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '是否上架 0 未上架 1 上架不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          delStatus: [
            { required: true, message: '是否删除不能为空', trigger: 'blur' }
          ],
          categoryId: [
            { required: true, message: '分类id不能为空', trigger: 'blur' }
          ],
          keyword: [
            { required: true, message: '关键字不能为空', trigger: 'blur' }
          ],
          introduce: [
            { required: true, message: '商品简介不能为空', trigger: 'blur' }
          ],
          unit: [
            { required: true, message: '单位不能为空', trigger: 'blur' }
          ],
          isPackage: [
            { required: true, message: '是否包邮不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.shopId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.shopId) {
            this.$http({
              url: this.$http.adornUrl(`/consumption/shop/info/${this.dataForm.shopId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.imgUrl = data.shop.imgUrl
                this.dataForm.shopName = data.shop.shopName
                this.dataForm.price = data.shop.price
                this.dataForm.count = data.shop.count
                this.dataForm.total = data.shop.total
                this.dataForm.sort = data.shop.sort
                this.dataForm.state = data.shop.state
                this.dataForm.status = data.shop.status
                this.dataForm.createTime = data.shop.createTime
                this.dataForm.updateTime = data.shop.updateTime
                this.dataForm.delStatus = data.shop.delStatus
                this.dataForm.categoryId = data.shop.categoryId
                this.dataForm.keyword = data.shop.keyword
                this.dataForm.introduce = data.shop.introduce
                this.dataForm.unit = data.shop.unit
                this.dataForm.isPackage = data.shop.isPackage
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/consumption/shop/${!this.dataForm.shopId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'shopId': this.dataForm.shopId || undefined,
                'imgUrl': this.dataForm.imgUrl,
                'shopName': this.dataForm.shopName,
                'price': this.dataForm.price,
                'count': this.dataForm.count,
                'total': this.dataForm.total,
                'sort': this.dataForm.sort,
                'state': this.dataForm.state,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'delStatus': this.dataForm.delStatus,
                'categoryId': this.dataForm.categoryId,
                'keyword': this.dataForm.keyword,
                'introduce': this.dataForm.introduce,
                'unit': this.dataForm.unit,
                'isPackage': this.dataForm.isPackage
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
